﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSM.Admin.DAO;

namespace VSM
{
    public partial class Login : System.Web.UI.Page
    {
        //MasterData obj = new MasterData();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            Session[Utils.AppConstants.ID_User] = null;
            Session[Utils.AppConstants.Password] = null;
            Session[Utils.AppConstants.ID_Dealer] = null;
            Session[Utils.AppConstants.U_Department] = null;
            if (!IsPostBack)
            {
                //LoadCompany();
                txtPassword.Attributes.Add("value", txtPassword.Text);
            }
        }

        private bool GetLogin(string userId, string passWord)
        {
            bool valid = false;

            MasterData masterData = new MasterData();

            DataSet dsUser = masterData.GetLogin(txtUserName.Text, txtPassword.Text, "VSM");

            if (dsUser != null && dsUser.Tables.Count > 0 && dsUser.Tables[0].Rows.Count > 0)
            {
                if (dsUser.Tables[0].Rows.Count == 1)
                {
                    try
                    {
                        Session[Utils.AppConstants.ID_User] = this.txtUserName.Text.Trim();
                        Session[Utils.AppConstants.Password] = this.txtPassword.Text.Trim();
                        Session[Utils.AppConstants.isAdmin] = dsUser.Tables[0].Rows[0]["isAdmin"];
                        Session[Utils.AppConstants.ID_Dealer] = dsUser.Tables[0].Rows[0]["ID_Dealer"];
                        Session[Utils.AppConstants.Default_Vendor] = dsUser.Tables[0].Rows[0]["Default_Vendor"];
                        Session[Utils.AppConstants.Email] = dsUser.Tables[0].Rows[0]["Email"];
                        Session[Utils.AppConstants.ID_Language] = dsUser.Tables[0].Rows[0]["ID_Language"];
                        Session[Utils.AppConstants.U_Department] = dsUser.Tables[0].Rows[0]["U_Department"];
                        //Get WareHouse - 5/16/2014 - Hieu START
                        string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                        if (dbType == Utils.AppConstants.DEALER)
                        {
                            DataSet dsWhs = masterData.GetWareHouseByUser(dsUser.Tables[0].Rows[0]["ID_Dealer"].ToString(), dsUser.Tables[0].Rows[0]["U_Department"].ToString(), dbType);
                            if (dsWhs != null && dsWhs.Tables.Count > 0 && dsWhs.Tables[0].Rows.Count > 0)
                            {
                                Session[Utils.AppConstants.WhsCode] = dsWhs.Tables[0].Rows[0]["WhsCode"].ToString();
                            }
                        }
                        //Get WareHouse - 5/16/2014 - Hieu END

                        valid = true;
                    }
                    catch (Exception ex)
                    {
                        if (int.Parse(dsUser.Tables[0].Rows[0]["ErrCode"].ToString()) == -9000)
                        {
                            this.lblMessage.Text = "Cannot connect to VSM DB. Please try again or contact to Admin IT to check.";
                        }
                        else if (int.Parse(dsUser.Tables[0].Rows[0]["ErrCode"].ToString()) == -9001)
                        {
                            this.lblMessage.Text = "Cannot connect to DEALER DB. Please try again or contact to Admin IT to check.";
                        }

                        valid = false;
                    }
                }
                else
                {
                    this.lblMessage.Text = "Invalid user.";
                    valid = false;
                }
            }
            else
            {
                this.lblMessage.Text = "User ID or Password is incorrect.";
                valid = false;
            }
            
            /*DataSet dsUser = SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.Text, "Select * From tbl_MasterUsers Where ID_User = '" + userId + "' and Password = '" + passWord + "' and Active = 1 ");
            if (dsUser != null && dsUser.Tables.Count > 0 && dsUser.Tables[0].Rows.Count > 0)
            {
                if (dsUser.Tables[0].Rows.Count == 1)
                {
                    Session[Utils.AppConstants.ID_User] = this.txtUserName.Text.Trim();
                    Session[Utils.AppConstants.Password] = this.txtPassword.Text.Trim();

                    //Session[Utils.AppConstants.Default_Vendor] = dsUser.Tables[0].Rows[0]["Default_Vendor"];

                    Session[Utils.AppConstants.Email] = dsUser.Tables[0].Rows[0]["Email"];
                    Session[Utils.AppConstants.ID_Language] = dsUser.Tables[0].Rows[0]["ID_Language"];
                    valid = true;
                }
                else
                {
                    this.lblMessage.Text = "Invalid user.";
                    valid = false;
                }
            }
            else
            {
                this.lblMessage.Text = "User ID or Password is incorrect.";
                valid = false;
            }*/

            return valid;
        }


        private bool SetConStrings()
        {
            bool smooth = false;
            DataSet dsConStrings = SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.Text, "Select * From tbl_DBLink ");

            if (dsConStrings != null && dsConStrings.Tables.Count > 0 && dsConStrings.Tables[0].Rows.Count > 0)
            {
                if (dsConStrings.Tables[0].Rows.Count == 2)
                {
                    //Session[Utils.AppConstants.ConStringDealer] = dsConStrings.Tables[0].Rows[0]["DBName"].ToString().Equals("DEALERDATA") ? dsConStrings.Tables[0].Rows[0]["ConnectionString"].ToString() : dsConStrings.Tables[0].Rows[1]["ConnectionString"].ToString();
                    //Session[Utils.AppConstants.ConStringVSM] = dsConStrings.Tables[0].Rows[0]["DBName"].ToString().Equals("VSMDATA") ? dsConStrings.Tables[0].Rows[1]["ConnectionString"].ToString() : dsConStrings.Tables[0].Rows[0]["ConnectionString"].ToString();
                    
                    smooth = true;
                }
                else
                {
                    this.lblMessage.Text = "Invalid ConnectionString.";
                    smooth = false;
                }
            }
            else
            {
                this.lblMessage.Text = "Cannot get connection string.";
                smooth = false;
            }

            return smooth;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (GetLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
                {
                    //if(SetConStrings())
                    Response.Redirect("homepage.aspx");
                }
                /*DataSet dsUser = obj.GetLogin(this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim(), this.drdOutlet.SelectedValue.ToString());
                if (dsUser != null && dsUser.Tables.Count > 0 && dsUser.Tables[0].Rows.Count > 0)
                {
                    if (dsUser.Tables[0].Columns.Count > 1)
                    {
                        //DataSet ds = obj.GetUserInfo(this.drdOutlet.SelectedValue.ToString(), this.txtUserName.Text.Trim());
                        //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        //{
                        //   Session[Utils.AppConstants.UserID]=ds.Tables[0].Rows[0]["UserID"].ToString(); 
                        //}
                        Session[Utils.AppConstants.IsSupperUser] = dsUser.Tables[0].Rows[0]["U_SUPERUSER"];
                        Session[Utils.AppConstants.OutletCode] = dsUser.Tables[0].Rows[0]["U_Outlet"];
                        Session[Utils.AppConstants.OutletName] = dsUser.Tables[0].Rows[0]["U_WhseName"];
                        Session[Utils.AppConstants.UserCode] = this.txtUserName.Text.Trim();
                        Session[Utils.AppConstants.CompanyCode] = this.drdOutlet.SelectedValue;
                        Session[Utils.AppConstants.CompanyName] = this.drdOutlet.SelectedItem.Text;
                        Session[Utils.AppConstants.Password] = this.txtPassword.Text.Trim();

                        Response.Redirect("Homepage.aspx");
                    }
                    else
                    {
                        this.lblMessage.Text = dsUser.Tables[0].Rows[0][0].ToString();
                    }
                }
                else
                {
                    this.lblMessage.Text = "User ID or Password is incorrect.";
                }*/
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = ex.Message;
            }
        }
    }
}